<template>
  <div class="login-container">
    <h2 class="login-title">在线学习系统</h2>
    <el-form
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
      label-width="120px"
      class="login-form"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="loginForm.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">登录</el-button>
        <el-button type="text" @click="resetForm">重置</el-button>
        <el-link type="text" @click="showPasswordReset">忘记密码？</el-link>
      </el-form-item>
      <div v-if="loginError" class="login-error">{{ loginError }}</div>
    </el-form>
    <el-dialog :visible.sync="passwordResetVisible" title="重置密码">
      <el-form
        :model="passwordResetForm"
        :rules="passwordResetRules"
        ref="passwordResetForm"
        label-width="120px"
      >
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            type="password"
            v-model="passwordResetForm.newPassword"
            placeholder="请输入新密码"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            type="password"
            v-model="passwordResetForm.confirmPassword"
            placeholder="请再次输入新密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handlePasswordResetSubmit"
            >提交</el-button
          >
          <el-button @click="passwordResetVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>  
    
  <script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [  
          { 
            required: true, 
            message: '请输入用户名', 
            trigger: 'blur' 
          },  
          { 
            min: 3, 
            max: 5, 
            message: '长度在 3 到 5 个字符', 
            trigger: 'blur'
          }  
        ],  
        password: [  
          { 
            required: true,
            message: '请输入密码',
            trigger: 'blur' 
           },  
          { 
            min: 5, 
            max: 18, 
            message: '长度在 5 到 18 个字符', 
            trigger: 'blur'
         }  
        ]   
      },
      loginError: "",
      passwordResetVisible: false,
      passwordResetForm: {
        newPassword: "",
        confirmPassword: "",
      },
      passwordResetRules: {
        newPassword: [
          { 
            required: true,
             message: "请输入新密码",
              trigger: "blur" 
          },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, 
            message: "请再次输入新密码",
             trigger: "blur"
          },
          { 
            validator: this.validateConfirmPassword, 
            trigger: "blur" 
          },
        ],
      },
    };
  },
  methods: {
    handleSubmit() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.login();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    login() {
      this.simulateLogin();
    },

    async simulateLogin() {
      // 假设这是你的登录API请求
      const response = await this.login();
      try {
        if (response.data.success) {
          // 登录成功
          this.$router.push("/Home"); // 假设登录成功后跳转到首页页面
        } else {
          // 登录失败，更新错误信息
          this.loginError = response.data.message || "登录失败，请重试。";
        }
      } catch (error) {
        // 处理请求错误，比如网络错误
        if (error.response) {
          // 请求已发出，但服务器响应的状态码不在 2xx 范围内
          this.loginError =
            error.response.data.message || "网络错误，请检查你的网络连接。";
        } else if (error.request) {
          // 请求已发出，但没有收到任何响应
          this.loginError = "请求已发送，但未收到响应。";
        } else {
          // 在设置请求时触发了某些错误
          this.loginError = "请求设置错误，请稍后再试。";
        }
      }
    },
    resetForm() {
      this.$refs.loginForm.resetFields();
    },
    showPasswordReset() {
      this.passwordResetVisible = true;
    },

    handlePasswordResetSubmit() {
      this.$refs.passwordResetForm.validate((valid) => {
        if (valid) {
          this.resetPassword();
        } else {
          console.log("密码重置表单验证失败！");
          return false;
        }
      });
    },
    resetPassword() {
      this.simulatePasswordReset();
    },
    simulatePasswordReset() {
      const userEmail = prompt("请输入您的电子邮件地址以重置密码：");
      function simulateApiRequest(email) {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            if (email === "") {
              // 用户未输入电子邮件地址
              reject("请输入有效的电子邮件地址");
            } else if (email === "invalid@example.com") {
              // 电子邮件地址无效或用户不存在
              reject("无效的电子邮件地址或用户不存在");
            } else if (email === "alreadyreset@example.com") {
              // 用户最近已经重置过密码
              reject("您最近已经重置过密码，请尝试登录");
            } else {
              // 假设其他情况都是成功的
              resolve("密码重置链接已发送到您的邮箱");
            }
          }, 1000);
        });
      }
      simulateApiRequest(userEmail)
        .then((message) => {
          console.log("密码重置成功：", message);
          this.$message.suceess("密码重置失败,请重试");
        })
        .catch((error) => {
          console.log("密码重置失败：", error);
          this.$message.info("密码重置失败,请重试");
        });
    },
  },
};
</script>  